package com.controller;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.plaf.synth.SynthSpinnerUI;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.pojo.Cztx;
import com.pojo.OUser;
import com.pojo.Sstatis;
import com.pojo.Statis;
import com.pojo.Wtd;
import com.pojo.tradeInfo;
import com.pojo.trecord;
import com.service.CztxService;

import com.service.WtdService;
import com.serviceImpl.WtdServiceImpl;
import com.alibaba.fastjson.JSON;
import com.login.CaptchaUtil.CaptchaUtil;
import com.mapper.AppMapper;
import com.mapper.AutoTradeMapper;
import com.mapper.CztxMapper;
import com.mapper.GoodMapper;
import com.mapper.TradeMapper;
import com.mapper.UsersMapper;
import com.mapper.WtdMapper;

@Controller
@RequestMapping("/")
public class UserController {
	protected final Log logger = LogFactory.getLog(getClass());
	@Resource
	private UsersMapper usersMapper;
	@Resource
	private AutoTradeMapper autoTradeMapper;
	@Resource
	private TradeMapper tradeMapper;
	@Resource
	private WtdMapper wtdMapper;
	@Resource
	private CztxMapper cztxMapper;	
	@Resource
	private CztxService cztxService;
	@Resource
	private GoodMapper goodMapper;
	@Resource
	private AppMapper appMapper;
	@Resource
	private WtdService wtdService;

	
	@RequestMapping("login")
	public String adminis(@Param("UserID") String UserID, @Param("Password") String Password,
			@Param("captcha") String captcha, HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		
		
		session.setAttribute("UserID", UserID);
		session.setAttribute("Password", Password);
		session.setAttribute("captcha", captcha);
		if (UserID == null || UserID == "") {
			model.addAttribute("error", "用户名不能为空！");
			return "login";
		} else if (Password == null || Password == "") {
			model.addAttribute("error", "密码不能为空！");
			return "login";
		} else if (captcha == null || captcha == "") {
			model.addAttribute("error", "验证码不能为空！");
			return "login";
		}

		HashMap<String, Object> users = new HashMap<String, Object>();
		users.put("UserID", UserID.trim());
		users.put("Password", Password.trim());

		users.put("captcha", captcha.trim());
		OUser user = usersMapper.getUserByUsernameAndPassword(users);
		// String code = (String) request.getSession().getAttribute("captcha");//

		// 得到session中的正确验证码

		String randomString = (String) session.getAttribute("randomString");
		session.setAttribute("user", user);
		//12.8
		int ip=usersMapper.selectip(UserID);
		if (user == null) {
			model.addAttribute("error", "用户名或密码错误！");
			return "login";
		} else if (!randomString.equals(captcha.toUpperCase())) {
			model.addAttribute("error", "验证码错误！");
			return "login";
		}else if(ip==0) {
			model.addAttribute("error", "你的注册申请正在审核，请耐心等待");
			return "login";
		}else {
			// return "redirect:/main.html";
			return "redirect:/main.html";
		}
	}

	@RequestMapping("main")
	public String admin(HttpServletRequest request, Model model) {
		List<Wtd> sy= usersMapper.selectindex();
		request.setAttribute("list", JSON.toJSONString(sy));
		//System.out.println( JSON.toJSONString(sy));
		List<Map> list=wtdMapper.selectgoodstype();
		//System.out.println("273"+JSON.toJSONString(list));
		request.setAttribute("list1", list);
		return "jsp/echart/main";
		//return "redirect:/main.html";
	}

	@RequestMapping("index")
	public String index(HttpServletRequest request, Model model) {
		List<Wtd> sy= usersMapper.selectindex();
		request.setAttribute("list", JSON.toJSONString(sy));
		//System.out.println(JSON.toJSONString(sy));

		return "jsp/index";
		//return "redirect:/login.html";
	}

	@RequestMapping("jsp/echart/kxingtu2")
	public String kxingtu2(HttpServletRequest request, Model model) {
		//String goods1 = request.getParameter("goods");
		//String goods = "\""+request.getParameter("goods") + "\"";
		String goods = request.getParameter("goods");
		//String goods = "\"goods1\"";
		//System.out.println(goods);
		List<Sstatis> us= goodMapper.selectkxian2(goods);
		//List<Sstatis>us= usersMapper.selectgupiao2();//搜索sstatis日线
		ArrayList al1=new ArrayList();
		for (Sstatis s : us) {
			ArrayList al2=new ArrayList();
			al2.add(s.getOpenprice());
			al2.add(s.getCloseprice());
			
			al2.add(s.getMinprice());
			al2.add(s.getMaxprice());
			al1.add(al2);
			
		}
		
		String str=JSON.toJSONString(al1);
		//System.out.println(JSON.toJSONString(us));
		request.setAttribute("list", str);
		List<Sstatis>us1= goodMapper.selecttime1(goods);
		ArrayList al=new ArrayList();
		for (Sstatis s : us1) {
			al.add(s.getOntime());
		}
		String str1=JSON.toJSONString(al);
		request.setAttribute("list1", str1);
		return "jsp/echart/kxingtu2";
	}
	
	@RequestMapping("jsp/echart/kxingtu")
	public String kxingtu(HttpServletRequest request, Model model) {
		String goods = request.getParameter("goods");
		System.out.println(goods);
		//List<Statis>us= usersMapper.selectgupiao();
		List<Statis>us= goodMapper.selectkxian(goods);
		ArrayList al1=new ArrayList();
		for (Statis s : us) {
			ArrayList al2=new ArrayList();
			al2.add(s.getOpenprice());
			al2.add(s.getCloseprice());
			
			al2.add(s.getMinprice());
			al2.add(s.getMaxprice());
			al1.add(al2);
			
		}
		
		String str=JSON.toJSONString(al1);
		request.setAttribute("list", str);
		List<Statis>us1= goodMapper.selecttime(goods);
		ArrayList al=new ArrayList();
		for (Statis s : us1) {
			al.add(s.getEverydate());
		}
		String str1=JSON.toJSONString(al);
		request.setAttribute("list1", str1);
		
		return "jsp/echart/kxingtu";
	}
	
	@RequestMapping("qingqiu1")
	@ResponseBody
	public String qingqiu1(HttpServletRequest request, Model model) {
		String goods = "\""+request.getParameter("goods") + "\"";
		//List<Sstatis>us= usersMapper.selectgupiao2();//搜索sstatis日线
		List<Sstatis>us= goodMapper.selectkxian2(goods);
		ArrayList al1=new ArrayList();
		for (Sstatis s : us) {
			ArrayList al2=new ArrayList();
			al2.add(s.getOpenprice());
			al2.add(s.getCloseprice());
			al2.add(s.getMinprice());
			al2.add(s.getMaxprice());
			al1.add(al2);
		}
		List<Sstatis>us1= usersMapper.selecttime2();
		ArrayList al=new ArrayList();
		for (Sstatis s : us1) {
			al.add(s.getOntime());
		}
		HashMap hm=new HashMap();
		hm.put("series", al1);
		hm.put("yAxis", al);
		return JSON.toJSONString(hm);
	}
	
	@RequestMapping("nobypt")
	public String nobypt(HttpServletRequest request, Model model) {
		List<tradeInfo> sy= usersMapper.selectbypt();
		request.setAttribute("list", JSON.toJSONString(sy));
		return "jsp/nobypt";
	}
	
	@RequestMapping("buylist")
	public String buylist(HttpServletRequest request) {
		HttpSession session = request.getSession();
		//分页
		int page=Integer.parseInt((request.getParameter("page")));
		int last=Integer.parseInt((request.getParameter("last")));
		int begin=(page-1)*5+1;
		
		OUser user = (OUser) session.getAttribute("user");
		String UserID = user.getUserID();
		HashMap mp = new HashMap();
		mp.put("begin", begin);
		mp.put("end", page*last);
		mp.put("UserID", UserID);
		List sy= usersMapper.searchquesbuy(mp);
		request.setAttribute("list", JSON.toJSONString(sy));
		
		int countques= appMapper.searchquesbuy(UserID);
		//System.out.println(JSON.toJSONString(list4));
		//trecord qb=(trecord)list4.get(0);
		request.setAttribute("countques", countques);
		request.setAttribute("page", page);
		return "jsp/buylist";
	}
	
	@RequestMapping("gr_money")
	public String gr_money(HttpServletRequest request) {
		HttpSession session = request.getSession();
		//分页
		int page=Integer.parseInt((request.getParameter("page")));
		int last=Integer.parseInt((request.getParameter("last")));
		int begin=(page-1)*5+1;
		
		OUser user = (OUser) session.getAttribute("user");
		String UserID = user.getUserID();
		HashMap mp = new HashMap();
		mp.put("begin", begin);
		mp.put("end", page*last);
		mp.put("UserID", UserID);
		List<trecord> sy= usersMapper.selectcjmingxi(mp);
		request.setAttribute("list", JSON.toJSONString(sy));
		int countques= appMapper.searchquesmx(UserID);
		//System.out.println(JSON.toJSONString(list4));
		//trecord qb=(trecord)list4.get(0);
		request.setAttribute("countques", countques);
		request.setAttribute("page", page);
		return "jsp/gr_money";
	}
	
	@RequestMapping("tiqu")
	public String tiqu() {
		return "jsp/tiqu";
	}
	
	@RequestMapping("zhuan")
	public String zhuan() {
		return "jsp/zhuan";
	}
	
	@RequestMapping("selllist")
	public String selllist(HttpServletRequest request) {
		HttpSession session = request.getSession();
		//分页
		int page=Integer.parseInt((request.getParameter("page")));
		int last=Integer.parseInt((request.getParameter("last")));
		int begin=(page-1)*5+1;
		OUser user = (OUser) session.getAttribute("user");
		String UserID = user.getUserID();
		HashMap mp = new HashMap();
		mp.put("begin", begin);
		mp.put("end", page*last);
		mp.put("UserID", UserID);
		List sy= usersMapper.selectselllist(mp);
		request.setAttribute("list", JSON.toJSONString(sy));
		
		int countques= appMapper.searchqueslist(UserID);
		//System.out.println(JSON.toJSONString(list4));
		//trecord qb=(trecord)list4.get(0);
		request.setAttribute("countques", countques);
		request.setAttribute("page", page);
		return "jsp/selllist";
	}
	
	@RequestMapping("bypt")
	public String bypt(HttpServletRequest request, Model model) {
		List<tradeInfo> sy= usersMapper.selectbypt();
		request.setAttribute("list", JSON.toJSONString(sy));
		Sstatis info = usersMapper.selectopenprice();
		request.setAttribute("info", info);
		List<Map> list=wtdMapper.selectgoodstype();
		request.setAttribute("list1", list);
		//System.out.println(JSON.toJSONString(list));
		return "jsp/bypt";
	}
	
	@RequestMapping("test")
	public void test() {
		List<Map> list=null;
		try {
			list = test1.fun();
		} catch (ParseException e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
		}
		for(Map a :list) {
			usersMapper.insertStatis(a);
		}
		
	}

	// 查询明细（取现、消费币、复投，积分）
	@RequestMapping("member")
	public String selectmember(HttpServletRequest request, Model model) {
		// 返回menber对象
		HttpSession session = request.getSession();
		OUser Ouser = (OUser) session.getAttribute("user");
		 System.out.println("aaaa"+Ouser.getUserID());
		OUser  user= usersMapper.selectmember(Ouser.getUserID());
		request.setAttribute("user", user);
		List<Map> list=wtdMapper.selectgoodstype();
		//System.out.println("273"+JSON.toJSONString(list));
		request.setAttribute("list", list);
		return "jsp/member";
	}
	
	
	
	

	@RequestMapping("jy")
	public String jy(HttpServletRequest request, Model model) {
		List<Wtd> sy= usersMapper.selectindex();
		request.setAttribute("list", JSON.toJSONString(sy));
		
		return "jsp/jy";
	}
	@RequestMapping("logout")
	public String logout(HttpServletRequest request) {
		request.getSession().removeAttribute("user");
		request.getSession().invalidate();
		return "redirect:/index.html";
	}


	@RequestMapping(value = "captcha", method = RequestMethod.GET)
	@ResponseBody
	public void captcha(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		CaptchaUtil.outputCaptcha(request, response);
	}

	
	//12 5 ll
	//tx
		@RequestMapping("tixian")
		public String tixian(HttpServletRequest request, Model model) {
			HttpSession session = request.getSession();
			OUser Ouser = (OUser) session.getAttribute("user");
			OUser  user= usersMapper.selectmember(Ouser.getUserID());
			request.setAttribute("user", user);
			return "jsp/tixian";
		}
		//cz
		@RequestMapping("chongzhi")
		public String chongzhi(HttpServletRequest request, Model model) {
			HttpSession session = request.getSession();
			OUser Ouser = (OUser) session.getAttribute("user");
			OUser  user= usersMapper.selectmember(Ouser.getUserID());
			request.setAttribute("user", user);
			return "jsp/chongzhi";
		}
		//提现 (xml)
		@RequestMapping("xjtixian")
		@ResponseBody
		public String xjtixian(HttpServletRequest request, Model model, Cztx tq) {
			HttpSession session = request.getSession();
			OUser Ouser = (OUser) session.getAttribute("user");
			OUser user = usersMapper.selectmember(Ouser.getUserID());
			int c=cztxMapper.selecttqb(Ouser.getUserID());
		   
			//System.out.println(user.getNickName());
			if(c>0) {
				String s="unpay";
				return s;
			}
			//System.out.println(JSON.toJSONString(tq));
			BigDecimal ec=user.getEncash();
			System.out.println(user.getEncash());
			float ec1=ec.floatValue();
			System.out.println(ec1);
			BigDecimal ep=tq.getTiqu();
			System.out.println(ep);
			float ep1=ep.floatValue();
			System.out.println(ep1);
			float b = ec1 - (float) ep1;
			BigDecimal balance = new BigDecimal(Double.toString(b));
			System.out.println(balance);
			if(ep1>ec1) {
				String s2="unable";
				return s2;
			}
			HashMap<String, Object> txrecord = new HashMap<String, Object>();
			for (int i = 0; i < 10; i++) {
				String uuid = UUID.randomUUID().toString().replaceAll("-", "");
				txrecord.put("swiftid", uuid);
			}

			txrecord.put("UserID", Ouser.getUserID());//yonghuid
			txrecord.put("tiqu", tq.getTiqu());//提取金额
			txrecord.put("bankname", tq.getBankname());//开户行
			txrecord.put("bankcd", tq.getBankcd());//卡号
			txrecord.put("usname", user.getNickName());//姓名
			
			//txrecord.put("balance", balance);//余额
			txrecord.put("comment1", "提现");//说明
			txrecord.put("amount",tq.getTiqu());//提现金额
			txrecord.put("balance",balance);
			
			System.out.println(txrecord);
			try {
				SimpleDateFormat sim2 = new SimpleDateFormat("HH:mm:ss");
				Date date2 = sim2.parse(sim2.format(new Date()));
				if (date2.before(sim2.parse("16:00:00")) && date2.after(sim2.parse("09:00:00"))) {
					cztxService.tx(txrecord);
				} else {
					String s1 = "not";
					return s1;
				}
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(txrecord);
			
	      return "jsp/tixian" ;
			
		}

		//充值
		@RequestMapping("xjchongzhi")
		@ResponseBody
		public String xjchongzhi(HttpServletRequest request, Model model,Cztx ct) {
			HttpSession session = request.getSession();
			OUser Ouser = (OUser) session.getAttribute("user");
			OUser  user= usersMapper.selectmember(Ouser.getUserID());
			BigDecimal yue=user.getEncash();
			BigDecimal ep=ct.getEpoint();
			float ye=yue.floatValue();
			//System.out.println("429"+ye);
			int ep1=ep.intValue();
			//System.out.println("431"+ep1);
			
			float bl=ye+(float)ep1;
			BigDecimal balance=new BigDecimal(Double.toString(bl));
			//System.out.println(bl);
			HashMap<String, Object> czrecord = new HashMap<String, Object>();
			for(int i=0;i<10;i++){
				String uuid = UUID.randomUUID().toString().replaceAll("-", "");
				czrecord.put("swiftid", uuid);
			}
			czrecord.put("UserID", Ouser.getUserID());
			czrecord.put("epoint",ct.getEpoint());
		//	czrecord.put("comment",ct.getComment());
			czrecord.put("img",ct.getImg());
			czrecord.put("comment","公司充值");
//			czrecord.put("amount",ct.getEpoint());
//			czrecord.put("balance",balance);
//			czrecord.put("comment1","公司充值");
			cztxMapper.addcz(czrecord);
			return "success";
		}
		//查看充值
		@RequestMapping("chakan")
		@ResponseBody
		public String chakan(HttpServletRequest request, Model model,Cztx mx) {
			HttpSession session = request.getSession();
			System.out.println(mx.getUserID());
			OUser Ouser = (OUser) session.getAttribute("user");
			List list=cztxMapper.selectcz(Ouser.getUserID());
			String str=JSON.toJSONString(list);
			System.out.println(str);
			//request.setAttribute("list",str);
			//System.out.println(list);
			return str;
		}
		//查看提现
		@RequestMapping("chakan1")
		@ResponseBody
		public String chakan1(HttpServletRequest request, Model model,Cztx mx) {
			HttpSession session = request.getSession();
			System.out.println(mx.getUserID());
			OUser Ouser = (OUser) session.getAttribute("user");
			List list=cztxMapper.selecttx(Ouser.getUserID());
		//	System.out.println("479:"+list);
			String str=JSON.toJSONString(list);
			//System.out.println("480:"+str);
			//request.setAttribute("list",str);
			//System.out.println(list);
			return str;
		}
		
		@RequestMapping("addUsersskip")
		public String addUsersskip(HttpServletRequest request) {

			return "jsp/insert";
		}

		@RequestMapping("addUsers")
		@ResponseBody
		public String adduser(HttpServletRequest request, OUser ou, Model model) {
			HttpSession session = request.getSession();
			String recommend = request.getParameter("anzhiName");
			System.out.println("519"+recommend);
			//List list1 = usersMapper.selectuserid(recommend);
			int c=usersMapper.selectanzhi(recommend);
			System.out.println(c);
			//System.out.println(list1);
//			String str1=JSON.toJSONString(list1);
//			System.out.println(str1);
			HashMap<String, Object> users = new HashMap<String, Object>();
			users.put("UserID", ou.getUserID());
			users.put("Password", ou.getPassword());
			users.put("NickName", ou.getNickName());
			users.put("UserCode", ou.getUserCode());
			users.put("BankAddress", ou.getBankAddress());
			users.put("BankCard", ou.getBankCard());
			users.put("BankName", ou.getBankName());
			users.put("anzhiName",ou.getAnzhiName());
			System.out.println(users);
			String uuid = ou.getUserID();
			OUser  user= usersMapper.selectmember(uuid);
			if(user!=null) {
				String s2="pp";
				return s2;
			}
			else if(c==0){
				String s1="noid";
				return s1;
			}
			else{
				usersMapper.addUsers(users);
			}
		    return "jsp/insert";
		}
		//首页的各个页面hzp 2017/12/8
		@RequestMapping("processMenu")
		public String processMenu(HttpServletRequest request, OUser ou, Model model) {
			String id = request.getParameter("id");
			String information = usersMapper.selectshouye(id);
			String title = usersMapper.selecttitle(id);
			String time = usersMapper.selectshijian(id);
			//System.out.println("600"+information);
			int iid = Integer.parseInt(id);
			//System.out.println("602"+id);
			if(iid==3) {
				return "jsp/about";
			}
			else if(iid==11) {
				return "jsp/list";
			}
			else if(iid==12) {
				request.setAttribute("title", "下载中心");
				request.setAttribute("time", "2017-12-01 09:19:09");
				request.setAttribute("list", "无");
				return "jsp/processMenu";
			}
			else	
				request.setAttribute("list", information);
				request.setAttribute("title", title);
				request.setAttribute("time", time);
			return "jsp/processMenu";
		}
		
		//获取用户个人的商品存量
		@RequestMapping("getgoodnum")
		@ResponseBody
		public String getgoodnum(HttpServletRequest request) {
			String goods = request.getParameter("goods");
			String UserID = request.getParameter("UserID");
			Map mp= new HashMap();
			mp.put("goods", goods);
			mp.put("UserID", UserID);
			System.out.println(JSON.toJSONString(mp));
			Wtd wtd=usersMapper.selectcgmx(mp);
			String sum;
			if(wtd==null) {
				sum="0";
				return sum;
			}
			int goodnum = wtd.getGoodnum()-wtd.getDjgoodnum();
			sum = String.valueOf(goodnum);
			System.out.println("goodsum"+sum);
			return sum;
		}
		//获得用户钞票剩余
		@RequestMapping("getmoneynum")
		@ResponseBody
		public String getmoneynum(HttpServletRequest request) {
			String UserID = request.getParameter("UserID");
			//System.out.println("UserID"+UserID);
			OUser  user= usersMapper.selectmember(UserID);
			String sum;
			if(user.getEncash()==null) {
				sum="0";
				return sum;
			}
			BigDecimal moneynum = user.getEncash();
			sum = String.valueOf(moneynum);
			return sum;
		}
		
		
}
